package com.sky.controller.admin;

import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Joey
 * @Description:
 * @date:2024/4/18 17:03
 */

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐业务相关接口")
public class SetMealController {


    @Autowired
    private SetMealService setMealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping()
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "setMealCache",key = "#setmealDTO.categoryId")
    public Result<String> addSetMeal(@RequestBody SetmealDTO setmealDTO){
        setMealService.insertSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> SetMealListPage(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setMealService.pageSetMeals(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改套餐售卖状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐售卖状态")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result<String> modifySetMealStatus(@PathVariable Integer status ,long id){
        log.info("禁用、启用 -> 套餐,{},{}",status,id);
        setMealService.modifyStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询菜品")
    public Result<SetmealVO> selectSetMealById(@PathVariable Integer id){
        log.info("菜品信息,{}",id);
        SetmealVO dish = setMealService.selectDishById(id);
        return Result.success(dish);
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     * @return
     */
    @PutMapping()
    @ApiOperation("修改套餐信息")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result<String> modifySetMealInfo(@RequestBody SetmealDTO setmealDTO){
        log.info("修改 -> 套餐,{}",setmealDTO.getName());
        setMealService.modifySetMealInfo(setmealDTO);
        return Result.success();
    }

    /**
     * 删除套餐信息
     * @param ids
     * @return
     */
    @DeleteMapping()
    @ApiOperation("删除套餐信息")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result<String> deleteSetMeal(@RequestParam List<Long> ids){
        log.info("删除套餐");
        setMealService.deleteSetMeal(ids);
        return Result.success();
    }
}
